package bookshop.controller.front;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Customer;
import bookshop.exception.ApplicationException;
import bookshop.service.CustomerService;
import bookshop.service.impl.CustomerServiceImpl;

public class CustomerServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
	    response.setCharacterEncoding("utf-8");
		
		String act=request.getParameter("act");
		
		if("input_customer".equals(act)){
			request.getRequestDispatcher("jsps/front/customer/input_customer.jsp").forward(request, response);
		}else if("register".equals(act)){
			
			String customerId=request.getParameter("customerid");
			String customerPwd=request.getParameter("customerpwd");
			String customerName=request.getParameter("customername");
			
			Customer customer =new Customer();
			
			customer.setCustomerId(customerId);
			customer.setCustomerPwd(customerPwd);
			customer.setCustomerName(customerName);
			customer.setMoodLevel("D");
			customer.setCustomerStatus("Y");
			
			CustomerService customerService =new CustomerServiceImpl();
			
			String errMsg=null;
			
			try {
				customerService.newCustomer(customer);
			} catch (ApplicationException e) {
				e.printStackTrace();
				errMsg = e.getMessage();
			}
			if(errMsg==null){
				request.getSession().setAttribute("loginedCustomer", customer);
				response.sendRedirect("workflow?act=mainscreen");
			}
			else{
				request.setAttribute("errMsg", errMsg);
				request.getRequestDispatcher("jsps/front/customer/input_customer.jsp").forward(request, response);
			}
		}else if("toLogin".equals(act)){//去登录界面
			request.getRequestDispatcher("jsps/front/customer/login_customer.jsp").forward(request, response);
		}else if("login".equals(act)){//尝试登录
			String customerId=request.getParameter("customerid");
			String customerPwd=request.getParameter("customerpwd");
			
			
			//测试
//			System.out.println(customerId);
//			System.out.println(customerPwd);

			
			CustomerService customerService=new CustomerServiceImpl();
			Customer customer = customerService.getCustomerById(customerId);
			String errMsg=null;
			
			if(customer==null){
				errMsg="没有这个用户存在，请检查！";
			}
			else{
				if(!customer.getCustomerPwd().equals(customerPwd))
					errMsg="密码不正确，请检查！";
			}
			if(errMsg==null){
				request.getSession().setAttribute("loginedCustomer", customer);
				response.sendRedirect("workflow?act=mainscreen");
			}
			else{
				request.setAttribute("errMsg", errMsg);
				request.getRequestDispatcher("jsps/front/customer/login_customer.jsp").forward(request, response);
			}
		}else if("mainscreen".equals(act)){//登陆成功，去主界面
			
			response.sendRedirect("workflow?act=mainscreen");
		}else if("logout".equals(act)){//登出系统
			request.getSession().removeAttribute("loginedCustomer");
			request.getSession().invalidate();
			request.getRequestDispatcher("/index.jsp").forward(request, response);
		}
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
